﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PlcUI_test.MyControls
{
    public partial class WelderMode : UserControl
    {
        public string VariableName { get; set; } = "";


        #region 按钮名称
        private string text1;
        private string text2;
        private string text3;
        private string text4;
        private string text5;
        private string text6;

        public string Text1
        {
            get { return text1; }
            set { text1 = value; radioButton1.Text = value; }
        }
        public string Text2
        {
            get { return text2; }
            set { text2 = value; radioButton2.Text = value; }
        }

        public string Text3
        {
            get { return text3; }
            set { text3 = value; radioButton3.Text = value; }
        }

        public string Text4
        {
            get { return text4; }
            set { text4 = value; radioButton4.Text = value; }
        }

        public string Text5
        {
            get { return text5; }
            set { text5 = value; radioButton5.Text = value; }
        }

        public string Text6
        {
            get { return text6; }
            set { text6 = value; radioButton6.Text = value; }
        }



        #endregion

        private short _workModel;


        /// <summary>
        /// 用来给下位机通信的接口
        /// </summary>
        [Description("用来给下位机通信的接口")]
        public short WorkModel
        {
            get { return _workModel; }
            set
            {
                _workModel = value;
                UpdateRadioButtons(_workModel);
            }
        }
        // 下位机读上来short
        private void UpdateRadioButtons(short value)
        {
            switch (value)
            {
                case 0:
                    radioButton1.Checked = true;
                    break;
                case 1:
                    radioButton2.Checked = true;
                    break;
                case 2:
                    radioButton3.Checked = true;
                    break;
                case 3:
                    radioButton4.Checked = true;
                    break;
                case 4:
                    radioButton5.Checked = true;
                    break;
                case 5:
                    radioButton6.Checked = true;
                    break;
                default:
                    break;
            }
        }

        public void Initialize()
        {
            radioButton1.Checked = true;  // 默认第一个选中
        }

        private string welderWorkModel;
        public string WelderWorkModel
        {
            get { return welderWorkModel; }
            private set { welderWorkModel = value; }
        }

        /// <summary>
        /// 根据H_W_M_WorkModel的值改变WelderWorkModel的值
        /// </summary>
        /// <param name="H_W_M_WorkModel"></param>
        private void ChangeWorkModel(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                WelderWorkModel = radioButton1.Name;
                _workModel = 0;
                ButtonCheckedChanged?.Invoke(this, radioButton1.Checked);
            }
            else if (radioButton2.Checked)
            {
                WelderWorkModel = radioButton2.Name;
                _workModel = 1;
                ButtonCheckedChanged?.Invoke(this, radioButton2.Checked);
            }
            else if (radioButton3.Checked)
            {
                WelderWorkModel = radioButton3.Name;
                _workModel = 2;
                ButtonCheckedChanged?.Invoke(this, radioButton3.Checked);
            }
            else if (radioButton4.Checked)
            {
                WelderWorkModel = radioButton4.Name;
                _workModel = 3;
                ButtonCheckedChanged?.Invoke(this, radioButton4.Checked);
            }
            else if (radioButton5.Checked)
            {
                WelderWorkModel = radioButton5.Name;
                _workModel = 4;
                ButtonCheckedChanged?.Invoke(this, radioButton5.Checked);
            }
            else if (radioButton6.Checked)
            {
                WelderWorkModel = radioButton6.Name;
                _workModel = 5;
                ButtonCheckedChanged?.Invoke(this, radioButton6.Checked);
            }
            
        }


        /// <summary>
        /// 这个地方只是事件的声明，不能加括号
        /// </summary>
        [Description("按钮值改变时触发")]
        public event EventHandler<bool> ButtonCheckedChanged;




        public WelderMode()
        {
            InitializeComponent();

            radioButton1.CheckedChanged += ChangeWorkModel;
            radioButton2.CheckedChanged += ChangeWorkModel;
            radioButton3.CheckedChanged += ChangeWorkModel;
            radioButton4.CheckedChanged += ChangeWorkModel;
            radioButton5.CheckedChanged += ChangeWorkModel;
            radioButton6.CheckedChanged += ChangeWorkModel;
        }
    }
}
